package com.xinmiao.test8.adaptere2;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jaeger.ninegridimageview.ItemImageClickListener;
import com.previewlibrary.GPreviewBuilder;
import com.previewlibrary.enitity.ThumbViewInfo;
import com.xinmiao.test8.MyNineImageGview;
import com.xinmiao.test8.R;
import com.xinmiao.test8.imagemanager.ImageLoaderOptions;
import com.xinmiao.test8.imagemanager.ImageManager;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${王俊强} on 2017/12/2.
 */

public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Datas> mDatas;

    public ListAdapter(List<Datas> datas) {
        mDatas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.itme, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder holder1 = (MyViewHolder) holder;
            Datas datas = mDatas.get(holder.getLayoutPosition()-1);
            holder1.bind(datas, holder);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }
}

class MyViewHolder extends RecyclerView.ViewHolder {
    private TextView mTextViewName, mTextViewContent;
    private ImageView mImageViewHead;
    private MyNineImageGview mMyNineImageGview;
    private NineImageAdapter mNineImageAdapter = new NineImageAdapter();
    private ArrayList<ThumbViewInfo> mThumbViewInfoList = new ArrayList<>();

    public MyViewHolder(View itemView) {
        super(itemView);
        AutoUtils.autoSize(itemView);

        mTextViewName = itemView.findViewById(R.id.itme_name);
        mTextViewContent = itemView.findViewById(R.id.itme_content);
        mImageViewHead = itemView.findViewById(R.id.itme_head);
        mMyNineImageGview = itemView.findViewById(R.id.itme_nineimage);
        mMyNineImageGview.setAdapter(mNineImageAdapter);
        mMyNineImageGview.setItemImageClickListener(new ItemImageClickListener<String>() {
            @Override
            public void onItemImageClick(Context context, ImageView imageView, int index, List<String> list) {
                computeBoundsBackward(list);//组成数据
                GPreviewBuilder.from((Activity) context)
                        .setData(mThumbViewInfoList)
                        .setCurrentIndex(index)
                        .setType(GPreviewBuilder.IndicatorType.Dot)
                        .start();//启动
            }
        });

    }

    private class OnitmeListener implements View.OnClickListener {
        private Datas mDatas;
        private int position;

        public void setDatas(Datas datas, int position) {
            this.mDatas = datas;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "" + position, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 查找信息
     *
     * @param list 图片集合
     */
    private void computeBoundsBackward(List<String> list) {
        ThumbViewInfo item;
        mThumbViewInfoList.clear();
        for (int i = 0; i < mMyNineImageGview.getChildCount(); i++) {
            View itemView = mMyNineImageGview.getChildAt(i);
            Rect bounds = new Rect();
            if (itemView != null) {
                ImageView thumbView = (ImageView) itemView;
                thumbView.getGlobalVisibleRect(bounds);
            }
            item = new ThumbViewInfo(list.get(i));
            item.setBounds(bounds);
            mThumbViewInfoList.add(item);
        }

    }


    public void bind(Datas datas, final RecyclerView.ViewHolder viewHolder) {
        mTextViewName.setText(datas.getName() + "");
        mTextViewContent.setText(datas.getContent() + "");
        ImageManager.getInstance().showImage(
                new ImageLoaderOptions.Builder(
                        mImageViewHead, datas.getImagHead())
                        .setPlaceholder(R.mipmap.ic_launcher)
                        .setSkipMemoryCache(true)
                        .build());
        mMyNineImageGview.setImagesData(datas.getUrls());

        mImageViewHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), ""+(viewHolder.getLayoutPosition()-1), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
